#include "FServer/MySQL/MySQLParser.h"

#include <sstream>

#include "FServer/MySQL/MySQLScanner.h"
#include "parser.hh"

namespace keeper_server {
MySQLParser::MySQLParser(MySQLStatement &stmt)
    : scanner_(new Scanner()),
      parser_(new Parser(*this, stmt)),
      location_(new location()) {}

MySQLParser::~MySQLParser() {
  delete parser_;
  delete scanner_;
  delete location_;
}

void MySQLParser::reset() {
  delete location_;
  location_ = new location();
}

int MySQLParser::parse() {
  scanner_->switch_streams(&std::cin, &std::cerr);
  parser_->parse();
  return 0;
}
int MySQLParser::parse(std::string &str) {
  std::istringstream iss(str);
  scanner_->switch_streams(&iss, &std::cerr);
  parser_->parse();
  return 0;
}

int MySQLParser::parse_file(std::string &path) {
  std::ifstream s(path.c_str(), std::ifstream::in);
  scanner_->switch_streams(&s, &std::cerr);

  parser_->parse();

  s.close();

  return 0;
}
}  // namespace keeper_server
